package controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import pojo.AppCategory;
import pojo.AppInfo;
import pojo.AppVersion;
import pojo.DataDictionary;
import service.AppInfoService;
import service.CategoryService;
import service.DataService;
import service.VersionService;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/app")
public class AppInfoController {
    @Resource
    private AppInfoService appInfoService;
    @Resource
    private VersionService versionService;
    @Resource

    private CategoryService categoryService;
    @Resource
    private DataService dataService;


    @RequestMapping("/list")
    public String appList(@RequestParam(defaultValue = "1") Integer pageIndex, String querySoftwareName, Integer queryFlatformId,
                          Integer queryCategoryLevel1, Integer queryCategoryLevel2, Integer queryCategoryLevel3, Model model, HttpSession session){
        PageHelper.startPage(pageIndex,5);
        List<AppInfo> appList = appInfoService.selList(querySoftwareName,1,queryCategoryLevel1,queryCategoryLevel2,queryCategoryLevel3,queryFlatformId);
        PageInfo pages = new PageInfo(appList);
        List appInfoList = pages.getList();
        model.addAttribute("pages",pages);
        List<DataDictionary> flatFormList = dataService.ddList("APP_FLATFORM");
        List<AppCategory> categoryLevel1List = categoryService.cateList(0);
        model.addAttribute("appInfoList",appInfoList);
        model.addAttribute("flatFormList",flatFormList);
        model.addAttribute("categoryLevel1List",categoryLevel1List);
        session.setAttribute("querySoftwareName",querySoftwareName);
        session.setAttribute("queryFlatformId",queryFlatformId);
        session.setAttribute("queryCategoryLevel1",0);
        session.setAttribute("queryCategoryLevel2",queryCategoryLevel2);
        session.setAttribute("queryCategoryLevel3",queryCategoryLevel3);
        return "/backend/applist";
    }

    @RequestMapping("/check")
    public String check(Integer aid, Integer vid, Model model){
        AppInfo appInfo = appInfoService.view(aid);
        AppVersion appVersion = versionService.sel(vid);
        model.addAttribute("appInfo",appInfo);
        model.addAttribute("appVersion",appVersion);
        return "/backend/appcheck";
    }

    //查看
    @RequestMapping("/view/{id}")
    public String view(@PathVariable Integer id,Model model){
        AppInfo appInfo = null;
        List<AppVersion> appVersionList = null;
        appInfo = appInfoService.view(id);
        appVersionList = versionService.selList(id);
        model.addAttribute("appVersionList",appVersionList);
        model.addAttribute("appInfo",appInfo);
        return "/developer/appinfoview";
    }

    //判断apk名字是否重名
    @GetMapping(value = "/apkExist",produces ={"application/json;charset=utf-8"})
    @ResponseBody
    public Object apkExist(String APKName){
        HashMap<String,String> map = new HashMap<String, String>();
        AppInfo appInfo = appInfoService.apkExist(APKName);
        if (StringUtils.isEmpty(APKName)){
            map.put("APKName","empty");

        }else if(appInfo != null){
            map.put("APKName","exist");
        }else {
            map.put("APKName","noexist");
        }
        return JSON.toJSONString(map);
    }

    //删除app
    @ResponseBody
    @GetMapping("/del")
    public Object delApp(Integer id){
        HashMap<String,Object> map = new HashMap<>();
        if (appInfoService.view(id) == null){
            map.put("delResult","notexist");
        }else if (appInfoService.del(id)>0){
            map.put("delResult","true");
        }else {
            map.put("delResult","false`");

        }
        return JSON.toJSONString(map);
    }

   /* @GetMapping(value = "/nameExist",produces ={"application/json;charset=utf-8"})
    @ResponseBody
    public Object nameExist(String softwareName){
        HashMap<String,String> map = new HashMap<String, String>();
        AppInfo appInfo = appInfoService.nameExist(softwareName);
        if (StringUtils.isEmpty(softwareName)){
            map.put("softwareName","empty");
        }else if(appInfo != null){
            map.put("softwareName","exist");
        }else {
            map.put("softwareName","noexist");
        }
        return JSON.toJSONString(map);
    }*/


}
